package com.dynamixsoftware.printershare.gmail;

import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

public class GmsProtos {
	public static final ProtoBufType EMAIL_ADDRESS;
	public static final String LABEL_ALL = "^all";
	public static final String LABEL_CHATS = "^b";
	public static final String LABEL_DRAFTS = "^r";
	public static final String LABEL_INBOX = "^i";
	public static final String LABEL_SENT = "^f";
	public static final String LABEL_SPAM = "^s";
	public static final String LABEL_STARRED = "^t";
	public static final String LABEL_TRASH = "^k";
	public static final String LABEL_UNREAD = "^u";
	public static final ProtoBufType REQUEST = new ProtoBufType();
	public static final ProtoBufType RESPONSE_CHUNK = new ProtoBufType();
	static final ProtoBufType RESPONSE_MESSAGE = new ProtoBufType();
	public static final ProtoBufType RESPONSE_PREAMBLE = new ProtoBufType();
	public static final ProtoBufType RESPONSE_QUERY = new ProtoBufType();
	public static final ProtoBufType RESPONSE_START_SYNC = new ProtoBufType();
	public static final ProtoBufType SENDER_INSTRUCTIONS = new ProtoBufType();

	static {
		EMAIL_ADDRESS = new ProtoBufType();
		REQUEST.addElement(531, 1, null);
		ProtoBufType localProtoBufType1 = new ProtoBufType("CONFIG");
		localProtoBufType1.addElement(533, 1, null);
		localProtoBufType1.addElement(1052, 2, null);
		localProtoBufType1.addElement(1052, 3, null);
		REQUEST.addElement(538, 2, localProtoBufType1);
		ProtoBufType localProtoBufType2 = new ProtoBufType("CONVERSATION_SYNC");
		ProtoBufType localProtoBufType3 = new ProtoBufType(
				"CONVERSATION_SYNC_CONVERSATION");
		localProtoBufType3.addElement(531, 1, null);
		localProtoBufType3.addElement(531, 2, null);
		localProtoBufType3.addElement(1043, 3, null);
		localProtoBufType2.addElement(1050, 1, localProtoBufType3);
		localProtoBufType2.addElement(1043, 2, null);
		localProtoBufType2.addElement(536, 3, null);
		localProtoBufType2.addElement(1043, 4, null);
		REQUEST.addElement(538, 3, localProtoBufType2);
		ProtoBufType localProtoBufType4 = new ProtoBufType("MAIN_SYNC");
		localProtoBufType4.addElement(531, 1, null);
		localProtoBufType4.addElement(531, 2, null);
		localProtoBufType4.addElement(533, 3, null);
		localProtoBufType4.addElement(533, 4, null);
		localProtoBufType4.addElement(533, 5, null);
		localProtoBufType4.addElement(536, 6, ProtoBuf.FALSE);
		localProtoBufType4.addElement(536, 7, new Long(0L));
		localProtoBufType4.addElement(536, 8, ProtoBuf.FALSE);
		localProtoBufType4.addElement(536, 9, ProtoBuf.FALSE);
		REQUEST.addElement(538, 4, localProtoBufType4);
		ProtoBufType localProtoBufType5 = new ProtoBufType("QUERY");
		localProtoBufType5.addElement(540, 1, null);
		localProtoBufType5.addElement(531, 2, null);
		localProtoBufType5.addElement(533, 3, null);
		localProtoBufType5.addElement(533, 4, null);
		REQUEST.addElement(538, 5, localProtoBufType5);
		ProtoBufType localProtoBufType6 = new ProtoBufType("START_SYNC");
		localProtoBufType6.addElement(531, 1, null);
		localProtoBufType6.addElement(531, 2, null);
		localProtoBufType6.addElement(531, 3, null);
		localProtoBufType6.addElement(531, 4, null);
		localProtoBufType6.addElement(536, 5, ProtoBuf.FALSE);
		localProtoBufType6.addElement(536, 6, ProtoBuf.FALSE);
		localProtoBufType6.addElement(536, 7, ProtoBuf.FALSE);
		REQUEST.addElement(538, 6, localProtoBufType6);
		new ProtoBufType("UPHILL_SYNC").addElement(531, 2, null);
		REQUEST.addElement(538, 7, localProtoBufType6);
		RESPONSE_CHUNK.addElement(539, 1, RESPONSE_PREAMBLE);
		RESPONSE_CHUNK.addElement(539, 4, RESPONSE_START_SYNC);
		RESPONSE_CHUNK.addElement(539, 11, RESPONSE_MESSAGE);
		RESPONSE_CHUNK.addElement(539, 15, RESPONSE_QUERY);
		RESPONSE_PREAMBLE.addElement(533, 1, null);
		RESPONSE_PREAMBLE.addElement(533, 2, null);
		RESPONSE_PREAMBLE.addElement(536, 3, ProtoBuf.FALSE);
		RESPONSE_PREAMBLE.addElement(536, 4, ProtoBuf.FALSE);
		RESPONSE_PREAMBLE.addElement(540, 5, "");
		RESPONSE_PREAMBLE.addElement(540, 6, new Long(0L));
		RESPONSE_QUERY.addElement(533, 1, null);
		RESPONSE_QUERY.addElement(531, 2, null);
		SENDER_INSTRUCTIONS.addElement(533, 1, null);
		SENDER_INSTRUCTIONS.addElement(533, 2, null);
		ProtoBufType localProtoBufType7 = new ProtoBufType("SENDER");
		localProtoBufType7.addElement(536, 1, null);
		localProtoBufType7.addElement(536, 2, null);
		localProtoBufType7.addElement(533, 3, null);
		localProtoBufType7.addElement(540, 4, null);
		SENDER_INSTRUCTIONS.addElement(1050, 3, localProtoBufType7);
		ProtoBufType localProtoBufType8 = new ProtoBufType("CONVERSATION");
		localProtoBufType8.addElement(531, 1, null);
		localProtoBufType8.addElement(531, 2, null);
		localProtoBufType8.addElement(531, 3, null);
		localProtoBufType8.addElement(540, 4, null);
		localProtoBufType8.addElement(540, 5, null);
		localProtoBufType8.addElement(533, 6, null);
		localProtoBufType8.addElement(531, 7, null);
		localProtoBufType8.addElement(533, 8, null);
		localProtoBufType8.addElement(536, 9, null);
		localProtoBufType8.addElement(539, 10, SENDER_INSTRUCTIONS);
		localProtoBufType8.addElement(1045, 11, null);
		RESPONSE_QUERY.addElement(1050, 3, localProtoBufType8);
		RESPONSE_START_SYNC.addElement(531, 1, null);
		RESPONSE_START_SYNC.addElement(531, 2, null);
		RESPONSE_START_SYNC.addElement(531, 3, null);
		ProtoBufType localProtoBufType9 = new ProtoBufType("LABEL");
		localProtoBufType9.addElement(533, 1, null);
		localProtoBufType9.addElement(540, 2, null);
		localProtoBufType9.addElement(540, 3, null);
		localProtoBufType9.addElement(533, 4, null);
		localProtoBufType9.addElement(533, 5, null);
		localProtoBufType9.addElement(533, 6, null);
		localProtoBufType9.addElement(540, 7, null);
		RESPONSE_START_SYNC.addElement(1050, 4, localProtoBufType9);
		RESPONSE_MESSAGE.addElement(531, 1, null);
		RESPONSE_MESSAGE.addElement(539, 2, EMAIL_ADDRESS);
		RESPONSE_MESSAGE.addElement(531, 3, null);
		RESPONSE_MESSAGE.addElement(531, 4, null);
		RESPONSE_MESSAGE.addElement(540, 5, null);
		RESPONSE_MESSAGE.addElement(540, 6, null);
		RESPONSE_MESSAGE.addElement(533, 7, null);
		RESPONSE_MESSAGE.addElement(536, 8, null);
		RESPONSE_MESSAGE.addElement(1051, 9, EMAIL_ADDRESS);
		RESPONSE_MESSAGE.addElement(1051, 10, EMAIL_ADDRESS);
		RESPONSE_MESSAGE.addElement(1051, 11, EMAIL_ADDRESS);
		RESPONSE_MESSAGE.addElement(1051, 12, EMAIL_ADDRESS);
		RESPONSE_MESSAGE.addElement(540, 13, null);
		RESPONSE_MESSAGE.addElement(1045, 14, null);
		ProtoBufType localProtoBufType10 = new ProtoBufType("ATTACHMENT");
		localProtoBufType10.addElement(540, 1, null);
		localProtoBufType10.addElement(540, 2, null);
		localProtoBufType10.addElement(540, 3, null);
		localProtoBufType10.addElement(540, 4, null);
		localProtoBufType10.addElement(533, 5, null);
		RESPONSE_MESSAGE.addElement(1050, 15, localProtoBufType10);
		RESPONSE_MESSAGE.addElement(537, 16, null);
		RESPONSE_MESSAGE.addElement(531, 17, null);
		RESPONSE_MESSAGE.addElement(537, 18, null);
		RESPONSE_MESSAGE.addElement(540, 19, null);
		EMAIL_ADDRESS.addElement(540, 1, null);
		EMAIL_ADDRESS.addElement(540, 2, null);
	}

	private static boolean accountDomainIsGmail(String paramString) {
		return ("".equals(paramString)) || ("gmail.com".equals(paramString))
				|| ("googlemail.com".equals(paramString));
	}

	private static String accountGetDomain(String paramString) {
		int i = paramString.indexOf("@");
		if (i != -1) {
			return paramString.substring(i + 1);
		}
		return "";
	}

	public static String[] createUrlAndCookie(String[] paramArrayOfString) {
		String str1 = accountGetDomain(paramArrayOfString[0]);
		boolean bool = accountDomainIsGmail(str1);
		StringBuilder localStringBuilder = new StringBuilder()
				.append("https://mail.google.com/");
		if (bool) {
		}
		String str3;
		for (String str2 = "mail";; str2 = "a/" + str1) {
			str3 = str2 + "/g/";
			if (!bool) {
				break;
			}
			String[] arrayOfString2 = new String[2];
			arrayOfString2[0] = str3;
			arrayOfString2[1] = ("GX=" + paramArrayOfString[1]);
			return arrayOfString2;
		}
		String[] arrayOfString1 = new String[2];
		arrayOfString1[0] = str3;
		arrayOfString1[1] = ("GXAS_SEC=" + str1 + "=" + paramArrayOfString[1]);
		return arrayOfString1;
	}

	public static ProtoBuf doRequest(String[] var0, ProtoBuf var1)
			throws Exception {
		int var2 = 0;

		StatusLine var8;
		int var9;
		do {
			BasicHttpParams var3 = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(var3, 15000);
			HttpConnectionParams.setSoTimeout(var3, 15000);
			DefaultHttpClient var4 = new DefaultHttpClient(var3);
			String[] var5 = createUrlAndCookie(var0);
			HttpPost var6 = new HttpPost(var5[0]
					+ "?version=25&clientVersion=25&allowAnyVersion=1");
			var6.setHeader("User-Agent",
					"Android-GmailProvider/210 (PrinterShare); gzip");
			var6.setHeader("Accept-Encoding", "gzip");
			var6.setHeader("Cookie", var5[1]);
			var6.setEntity(new ByteArrayEntity(var1.toByteArray()));
			HttpResponse var7 = var4.execute(var6);
			var8 = var7.getStatusLine();
			var9 = var8.getStatusCode();
			if (var9 == 200) {
				Header var10 = var7.getEntity().getContentEncoding();
				Object var11;
				if (var10 != null && "gzip".equals(var10.getValue())) {
					var11 = new GZIPInputStream(var7.getEntity().getContent());
				} else {
					var11 = var7.getEntity().getContent();
				}
				ProtoBuf localProtoBuf = new ProtoBuf(RESPONSE_CHUNK);
				localProtoBuf.parse((InputStream) var11);
				return localProtoBuf;
			}

			if (var9 != 502) {
				break;
			}

			++var2;
		} while (var2 < 3);

		String var14 = var8.getReasonPhrase();
		String var15 = accountGetDomain(var0[0]);
		StringBuilder var16 = (new StringBuilder()).append("HTTP error ")
				.append(var9);
		String var17;
		if (var14 != null) {
			var17 = ". " + var14;
		} else {
			var17 = "";
		}

		throw new Exception(var16.append(var17).append(". Domain of email is ")
				.append(var15).toString());
	}

	public static InputStream loadAttachment(String[] var0, String var1,
			String var2) throws Exception {
		int var3 = 0;

		StatusLine var9;
		int var10;
		do {
			BasicHttpParams var4 = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(var4, 15000);
			HttpConnectionParams.setSoTimeout(var4, 15000);
			DefaultHttpClient var5 = new DefaultHttpClient(var4);
			String[] var6 = createUrlAndCookie(var0);
			HttpGet var7 = new HttpGet(
					var6[0]
							+ "?version=25&clientVersion=25&allowAnyVersion=1&view=att&messageId="
							+ var1 + "&partId=" + var2
							+ "&maxWidth=96&maxHeight=96&showOriginal=1");
			var7.setHeader("User-Agent", "AndroidDownloadManager");
			var7.setHeader("Cookie", var6[1]);
			HttpResponse var8 = var5.execute(var7);
			var9 = var8.getStatusLine();
			var10 = var9.getStatusCode();
			if (var10 == 200) {
				return var8.getEntity().getContent();
			}

			if (var10 != 502) {
				break;
			}

			++var3;
		} while (var3 < 3);

		String var11 = var9.getReasonPhrase();
		String var12 = accountGetDomain(var0[0]);
		StringBuilder var13 = (new StringBuilder()).append("HTTP error ")
				.append(var10);
		String var14;
		if (var11 != null) {
			var14 = ". " + var11;
		} else {
			var14 = ".";
		}

		throw new Exception(var13.append(var14).append(". Domain of email is ")
				.append(var12).toString());
	}
}
